/******************************************************************************
 * * ui_nvm.h - header of nvm function
 *
 * *(C) Copyright 2019 Asr International Ltd.
 * * All Rights Reserved
 * ******************************************************************************/
#ifndef _UI_NVM_H_
#define _UI_NVM_H_

#ifdef __cplusplus
extern "C" {
#endif /* __cpluscplus */
#include <stdio.h>
#include "ui_type.h"
#include "ui_nvm_interface.h"
#include "string.h"
#include "ui_log.h"

// #define NVM_LINUX                          1
// #define UI_FS_USED                              0

/* definition of section length */
#define NV_SECTION_UI_FRAMEWORK_LEN             (sizeof(NV_UI_Framework_t))
#define NV_SECTION_UI_CALL_LEN                  (sizeof(NV_UI_Call_t))
#define NV_SECTION_UI_CALL_LOG_LEN              (sizeof(NV_UI_Call_Log_t))
#define NV_SECTION_UI_SETTINGS_LEN              (sizeof(NV_UI_Settings_t))
#define NV_SECTION_UI_CONTACTS_LIST_LEN         (sizeof(NV_UI_Contacts_Contact_List_t))
#define NV_SECTION_UI_CONTACTS_BITMAP_LEN       (sizeof(NV_UI_Contacts_Bitmap_t))
#define NV_SECTION_UI_SPEED_DIAL_LIST_LEN       (sizeof(NV_UI_Contacts_Speed_Dial_List_t))
#define NV_SECTION_UI_NUM_SCRN_LIST_LEN         (sizeof(NV_UI_Contacts_Num_Scrn_List_t))
#define NV_SECTION_UI_ALARM_LEN                 (sizeof(NV_UI_Alarm_t))
#define NV_SECTION_UI_CALENDAR_LEN              (sizeof(NV_UI_Calendar_t))
#define NV_SECTION_UI_SMS_LEN                   (sizeof(NV_UI_Sms_t))
#define NV_SECTION_UI_SMS_DIST_LIST_LEN         (sizeof(NV_UI_Sms_Distribution_List_t))
#define NV_SECTION_UI_SMS_DIST_CONT_LIST_LEN    (sizeof(NV_UI_Sms_Distribution_Contacts_List_t))
#define NV_SECTION_UI_SMS_DRAFTS_LEN            (sizeof(NV_UI_Sms_Drafts_t))
#define NV_SECTION_UI_AUDIO_PLAYER_MODE_LEN     (sizeof(NV_UI_Audio_Player_t))
#define NV_SECTION_UI_BLUETOOTH_LEN             (sizeof(NV_UI_Bluetooth_t))
#define NV_SECTION_LEN(sectionid)    (sectionid ## _LEN)

/* definition of section offset */
#define NV_SECTION_UI_SETTINGS_OFFSET              (0)
#define NV_SECTION_UI_FRAMEWORK_OFFSET             (NV_SECTION_UI_SETTINGS_OFFSET + \
                                                    NV_SECTION_UI_SETTINGS_LEN)
#define NV_SECTION_UI_CALL_OFFSET                  (NV_SECTION_UI_FRAMEWORK_OFFSET + \
                                                    NV_SECTION_UI_FRAMEWORK_LEN)
#define NV_SECTION_UI_CALL_LOG_OFFSET              (NV_SECTION_UI_CALL_OFFSET + \
                                                    NV_SECTION_UI_CALL_LEN)
#define NV_SECTION_UI_CONTACTS_LIST_OFFSET         (NV_SECTION_UI_CALL_LOG_OFFSET + \
                                                    NV_SECTION_UI_CALL_LOG_LEN)
#define NV_SECTION_UI_CONTACTS_BITMAP_OFFSET       (NV_SECTION_UI_CONTACTS_LIST_OFFSET + \
                                                    NV_SECTION_UI_CONTACTS_LIST_LEN)
#define NV_SECTION_UI_SPEED_DIAL_LIST_OFFSET       (NV_SECTION_UI_CONTACTS_BITMAP_OFFSET + \
                                                    NV_SECTION_UI_CONTACTS_BITMAP_LEN)
#define NV_SECTION_UI_NUM_SCRN_LIST_OFFSET         (NV_SECTION_UI_SPEED_DIAL_LIST_OFFSET + \
                                                    NV_SECTION_UI_SPEED_DIAL_LIST_LEN)
#define NV_SECTION_UI_ALARM_OFFSET                 (NV_SECTION_UI_NUM_SCRN_LIST_OFFSET + \
                                                    NV_SECTION_UI_NUM_SCRN_LIST_LEN)
#define NV_SECTION_UI_CALENDAR_OFFSET              (NV_SECTION_UI_ALARM_OFFSET + \
                                                    NV_SECTION_UI_ALARM_LEN)
#define NV_SECTION_UI_SMS_OFFSET                   (NV_SECTION_UI_CALENDAR_OFFSET + \
                                                    NV_SECTION_UI_CALENDAR_LEN)
#define NV_SECTION_UI_SMS_DIST_LIST_OFFSET         (NV_SECTION_UI_SMS_OFFSET + \
                                                    NV_SECTION_UI_SMS_LEN)
#define NV_SECTION_UI_SMS_DIST_CONT_LIST_OFFSET    (NV_SECTION_UI_SMS_DIST_LIST_OFFSET + \
                                                    NV_SECTION_UI_SMS_DIST_LIST_LEN)
#define NV_SECTION_UI_SMS_DRAFTS_OFFSET            (NV_SECTION_UI_SMS_DIST_CONT_LIST_OFFSET + \
                                                    NV_SECTION_UI_SMS_DIST_CONT_LIST_LEN)
#define NV_SECTION_UI_AUDIO_PLAYER_MODE_OFFSET     (NV_SECTION_UI_SMS_DRAFTS_OFFSET + \
                                                    NV_SECTION_UI_SMS_DRAFTS_LEN)
#define NV_SECTION_UI_BLUETOOTH_OFFSET             (NV_SECTION_UI_AUDIO_PLAYER_MODE_OFFSET + \
                                                    NV_SECTION_UI_AUDIO_PLAYER_MODE_LEN)
#define NV_SECTION_OFFSET(sectionid)    (sectionid ## _OFFSET)

typedef lv_fs_file_t ui_file_t;
typedef lv_fs_dir_t  ui_dir_t;
#define UI_FILE_READ_ONLY     LV_FS_MODE_RD
#define UI_FILE_WRITE_ONLY    LV_FS_MODE_WR
#define UI_FILE_RD_APPEND     (LV_FS_MODE_RD | LV_FS_MODE_APPEND)
#define UI_FILE_APPEND        LV_FS_MODE_APPEND
#define UI_FILE_READ_WRITE    (LV_FS_MODE_RD | LV_FS_MODE_WR)
#define UI_FILE_OPEN(p, m)   \
    ({                       \
        ui_file_t *f;        \
        f = lv_mem_alloc(sizeof(ui_file_t)); \
        memset(f,0,sizeof(ui_file_t)); \
        lv_fs_open(f, p, m); \
        (f);                 \
    }                        \
    )

#define UI_FILE_CLOSE(f)      \
    ({                        \
        lv_fs_close(f);       \
        lv_mem_free(f);       \
    }                         \
    )
#define UI_FILE_SEEK          lv_fs_seek
#define UI_FILE_READ(f, b, l)          \
    ({                                 \
        uint32_t Ret_Len;                \
        lv_fs_read(f, b, l, &Ret_Len); \
        (Ret_Len);                     \
    }                                  \
    )

#define UI_FILE_WRITE(f, b, l)          \
    ({                                  \
        uint32_t Ret_Len;                 \
        lv_fs_write(f, b, l, &Ret_Len); \
        (Ret_Len);                      \
    }                                   \
    )

#define UI_FILE_TELL                             lv_fs_tell
#define UI_FILE_RENAME                           lv_fs_rename
#define UI_FILE_REMOVE                           lv_fs_remove
#define UI_FILE_SIZE                             lv_fs_size
#define UI_DIR_OPEN(p)        \
    ({                        \
        ui_dir_t *d;          \
        lv_fs_dir_open(d, p); \
        (d);                  \
    }                         \
    )

#define NV_SECTION_ROOT_DIR                      "C:/"

#define NV_MAX_SECTION_PATH_NAME                 50
#define NV_SECTION_UI_SETTINGS_FILE              "ui_settings_phone.nv"
#define NV_SECTION_UI_FRAMEWORK_FILE             "ui_framework.nv"
#define NV_SECTION_UI_CALL_FILE                  "ui_call.nv"
#define NV_SECTION_UI_CALL_LOG_FILE              "ui_call_log.nv"
#define NV_SECTION_UI_CONTACTS_LIST_FILE         "ui_contact_list.nv"
#define NV_SECTION_UI_CONTACTS_BITMAP_FILE       "ui_contact_bitmap.nv"
#define NV_SECTION_UI_SPEED_DIAL_LIST_FILE       "ui_speed_dial.nv"
#define NV_SECTION_UI_NUM_SCRN_LIST_FILE         "ui_num_scrn.nv"
#define NV_SECTION_UI_ALARM_FILE                 "ui_alarm.nv"
#define NV_SECTION_UI_CALENDAR_FILE              "ui_calendar.nv"
#define NV_SECTION_UI_SMS_FILE                   "ui_sms.nv"
#define NV_SECTION_UI_SMS_DIST_LIST_FILE         "ui_sms_dist_list.nv"
#define NV_SECTION_UI_SMS_DIST_CONT_LIST_FILE    "ui_sms_dist_cont.nv"
#define NV_SECTION_UI_SMS_DRAFTS_FILE            "ui_sms_drafts.nv"
#define NV_SECTION_UI_AUDIO_PLAYER_MODE_FILE     "ui_audio_player_mode.nv"
#define NV_SECTION_UI_BLUETOOTH_FILE             "ui_bluetoorh.nv"
#define NV_SECTION_FILE_PATH(sectionid)    (sectionid ## _FILE)

extern char *UI_NV_Get_Section_path(NV_UI_SECTION_ID SectionId);

#ifdef __cplusplus
}
#endif /* __cpluscplus */
#endif /* end of _UI_NVM_H_ */
